//
//  HEServerCommentListViewController.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/11/11.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEServerCommentListViewController.h"
#import "HEGoodsDetailsCommentsCell.h"
#import "HEGoodsCommentModel.h"
#import "HEServerCommentCell.h"
#import "HEServerCommentModel.h"


@interface HEServerCommentListViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) GDTableView *tableView;
@property (nonatomic, strong) NSMutableArray <HEServerCommentModel *> * dataSourceArr;

@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, assign) NSInteger pageSize;
@end

@implementation HEServerCommentListViewController


- (NSMutableArray<HEServerCommentModel *> *)dataSourceArr {
    if (!_dataSourceArr) {
        _dataSourceArr = [NSMutableArray array];
    }
    return _dataSourceArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"评价";
    
    _pageNum = 1;
    _pageSize = 10;
        
    [self setCustomTableView:self.tableView];
    [self showEmpty];
 
    [self loadData];
}

- (GDTableView *)tableView {
    if (!_tableView) {
        _tableView = [[GDTableView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight , KScreenWidth, KScreenHeight - ZXNavBarHeight) style:UITableViewStylePlain];
        _tableView.separatorColor = KHexColor(@"EEEEEE");
        _tableView.scrollsToTop = YES;
        _tableView.tableFooterView = [[UIView alloc] init];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.backgroundColor = KWhiteColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        

        __weak __typeof(&*self)weakSelf = self;
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{

            weakSelf.isFromHeadRefresh = YES;
            weakSelf.pageNum = 1;
            [weakSelf loadData];
        }];
        
        self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
            weakSelf.isFromHeadRefresh = NO;
            weakSelf.pageNum += 1;
            [weakSelf loadData];
        }];
        
        [_tableView registerClass:[HEServerCommentCell class] forCellReuseIdentifier:@"HEServerCommentCell"];
    }
    return _tableView;
}

 

#pragma mark -

- (void)loadData {
//
//    HEPageRequest *params = [HEPageRequest new];
//    params.pageNum = _pageNum;
//    params.pageSize = _pageSize;
//    [params addParmas:_serverId key:@"serverId"];

    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:[NSNumber numberWithInteger:_pageNum] forKey:@"pageNum"];
    [params setValue:[NSNumber numberWithInteger:_pageSize] forKey:@"pageSize"];
    [params setValue:_serverId forKey:@"serverId"];

    [WOAFNetWorkingHandle POSTNoHUDWithBaseUrlByKeyValue:KServerUrl suffixUrl:KserverCommentList parameters:params success:^(id  _Nonnull responseObject) {
        NSLog(@"response：  %@", [responseObject mj_JSONString]);

        if (self.isFromHeadRefresh) {
            [self.dataSourceArr removeAllObjects];
        }
        
        HEMallPageResponse *response = [HEMallPageResponse mj_objectWithKeyValues:responseObject];
        [self.dataSourceArr addObjectsFromArray:[HEServerCommentModel mj_objectArrayWithKeyValuesArray:response.data.records]];
        
        [self.tableView reloadData];

        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];

        if (self.dataSourceArr.count == 0) {
            [self showEmpty];
        }else {
            if (response.data.records.count == 0) {
                [Dialog toastBottom:@"没有更多数据了~"];
            }
        }
        
    } failure:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HEServerCommentCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"HEServerCommentCell"];
    cell.model = _dataSourceArr[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [tableView fd_heightForCellWithIdentifier: @"HEServerCommentCell" cacheByIndexPath:indexPath configuration:^(HEServerCommentCell *cell) {
        cell.model = self.dataSourceArr[indexPath.row];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    

}

#pragma mark -


@end
